//
//  DrawSixLineStar.m
//  六棱形
//
//  Created by 吕清毅 on 2018/3/28.
//  Copyright © 2018年 lqy. All rights reserved.
//

#import "DrawSixLineStar.h"

#define Margin 50
#define r 120
#define Width [UIScreen mainScreen].bounds.size.width
#define Height [UIScreen mainScreen].bounds.size.height
#define zzColor(r, g, b)  [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define zzColorRGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
// 随机色
#define zzRandomColor  zzColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)

@interface DrawSixLineStar()

@property (nonatomic, assign) CGPoint point_A;
@property (nonatomic, assign) CGPoint point_B;
@property (nonatomic, assign) CGPoint point_C;
@property (nonatomic, assign) CGPoint point_D;
@property (nonatomic, assign) CGPoint point_E;
@property (nonatomic, assign) CGPoint point_F;
@end

@implementation DrawSixLineStar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        self.zzWidth = frame.size.width;
        self.zzHeight = frame.size.height;
    }
    
    return self;
}

#pragma mark -- 搭建六芒星
- (void)drawLiuMangXing:(NSArray *)x_itemArr and:(NSArray *)array
{
    UILabel *title = [[UILabel alloc]init];
    title.frame =CGRectMake(0, 20, _zzWidth, 30);
    title.text = @"王者荣耀六芒星战绩";
    title.textAlignment = NSTextAlignmentCenter;
    title.font = [UIFont systemFontOfSize:18];
    title.textColor = [UIColor orangeColor];
    [self addSubview:title];
    
    float myIndex = (_zzWidth - 2*r*cos(DEGREES_TO_RADIANS(30)))/2;
    
    
    CGPoint starPoint = CGPointMake(myIndex, 2*Margin+60);
    CGPoint endPoint;
    float tan =  cos(DEGREES_TO_RADIANS(30));;
    float b_x = myIndex+r*tan;
    float b_y = 2*Margin;
    float c_x = myIndex + 2* r*tan;
    float c_y = starPoint.y;
    float d_x = c_x;
    float d_y = c_y + r;
    float e_x = b_x;
    float e_y = b_y + 2*r;
    float f_x = starPoint.x;
    float f_y = starPoint.y + r;
    
    //赋值
    self.point_A = starPoint;
    self.point_B = CGPointMake(b_x, b_y);
    self.point_C = CGPointMake(c_x, c_y);
    self.point_D = CGPointMake(d_x, d_y);
    self.point_E = CGPointMake(e_x, e_y);
    self.point_F = CGPointMake(f_x, f_y);
    
    
    NSArray *xArray =@[@(starPoint.x),@(b_x),@(c_x),@(d_x),@(e_x),@(f_x),@(starPoint.x)];
    NSArray *yArray = @[@(starPoint.y),@(b_y),@(c_y),@(d_y),@(e_y),@(f_y),@(starPoint.y)];
    
    
    for (int i = 0; i < xArray.count; i++) {
        
        //  float x =  [xArray[i] floatValue];
        
        endPoint = CGPointMake([xArray[i] floatValue], [yArray[i]floatValue]);
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:starPoint];
        [path addArcWithCenter:endPoint radius:1.5 startAngle:0 endAngle:2*M_PI clockwise:YES];
        [path addLineToPoint:endPoint];
        
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.path = path.CGPath;
        layer.fillColor = [UIColor clearColor].CGColor;
        layer.strokeColor = [UIColor cyanColor].CGColor;
        layer.lineWidth = 1;
        [self.layer addSublayer:layer];
        
        starPoint = endPoint;
        
        //设置标题
        
        
        //绘制虚线
        [self drawLine:endPoint andIndex:i andView:self];
        
        //绘制图形
        [self drawShapeLayer:self with:array];
    }
}


#pragma mark -- 绘制图形  设置随机数
- (void)drawShapeLayer:(UIView *)view with:(NSArray *)levelArray
{
    //根号3  1.73205
    //将等级转为数字
    NSMutableArray *numberArray = [self getLevelToNumber:levelArray];
    NSMutableArray *xArr = [[NSMutableArray alloc]init];
    NSMutableArray *yArr = [[NSMutableArray alloc]init];
    
    CGPoint indexPoint;
    for (int i = 0; i < numberArray.count; i++)
    {
        
        float x = 0,y = 0,index = 0;
        int number =  [numberArray[i] intValue];
        
        switch (i) {
            case 0:
            {
                if (number == r) {
                    x = self.point_A.x;
                    y = self.point_A.y;
                }else
                {
                    index = r - number;
                    x = self.point_A.x + index*cos(DEGREES_TO_RADIANS(30));
                    y = self.point_A.y + index*sin(DEGREES_TO_RADIANS(30));
                }
                indexPoint.x = x;
                indexPoint.y = y;
                [xArr addObject:@(x)];
                [yArr addObject:@(y)];
                
                break;
            }
            case 1:
            {
                x = self.point_B.x;
                if (number == r) {
                    y = self.point_B.y;
                }
                else
                {
                    index = r - number;
                    y = self.point_B.y + index;
                }
                [xArr addObject:@(x)];
                [yArr addObject:@(y)];
                break;
            }
            case 2:
            {
                if (number == r) {
                    x = self.point_C.x;
                    y = self.point_C.y;
                }else
                {
                    index = r - number;
                    x = self.point_C.x - index*cos(DEGREES_TO_RADIANS(30));
                    y = self.point_C.y + index*sin(DEGREES_TO_RADIANS(30));
                }
                [xArr addObject:@(x)];
                [yArr addObject:@(y)];
                
                break;
            }
            case 3:
            {
                if (number == r) {
                    x = self.point_D.x;
                    y = self.point_D.y;
                }else
                {
                    index = r - number;
                    x = self.point_D.x - index*cos(DEGREES_TO_RADIANS(30));
                    y = self.point_D.y - index*sin(DEGREES_TO_RADIANS(30));
                }
                [xArr addObject:@(x)];
                [yArr addObject:@(y)];
                
                break;
            }
            case 4:
            {
                x = self.point_E.x;
                if (number == r) {
                    y = self.point_E.y;
                }
                else
                {
                    index = r - number;
                    y = self.point_E.y - index;
                }
                [xArr addObject:@(x)];
                [yArr addObject:@(y)];
                break;
            }
            case 5:
            {
                if (number == r) {
                    x = self.point_F.x;
                    y = self.point_F.y;
                }else
                {
                    index = r - number;
                    x = self.point_F.x + index*cos(DEGREES_TO_RADIANS(30));
                    y = self.point_F.y - index*sin(DEGREES_TO_RADIANS(30));
                }
                [xArr addObject:@(x)];
                [yArr addObject:@(y)];
                
                break;
            }
            default:
                break;
        }
    }
    
    [xArr addObject:@(indexPoint.x)];
    [yArr addObject:@(indexPoint.y)];
    CGPoint startPoint = indexPoint;
    CGPoint endPoint;

    //角度
//    CGFloat startAngle = 0;
//    CGFloat endAngle;
//    float myIndex = (_zzWidth - 2*r*cos(DEGREES_TO_RADIANS(30)))/2;
//    CGPoint yPoint = CGPointMake(myIndex+r*cos(DEGREES_TO_RADIANS(30)), 2*Margin+r);
//    NSArray *x_itemArr = @[@"打野S",@"打野S",@"打野S",@"打野S",@"打野S",@"打野S",@"打野S"];
    
    for (int i = 0; i < xArr.count; i++) {
        
        endPoint = CGPointMake([xArr[i] floatValue], [yArr[i]floatValue]);
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:startPoint];
       // [path addArcWithCenter:endPoint radius:1.5 startAngle:0 endAngle:2*M_PI clockwise:YES];
        [path addLineToPoint:endPoint];
        
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.path = path.CGPath;
        layer.backgroundColor = [[UIColor orangeColor]CGColor];
        layer.fillColor = [[UIColor clearColor]CGColor];
        layer.strokeColor = [UIColor redColor].CGColor;
        //layer.fillColor = zzRandomColor.CGColor;
        layer.lineWidth = 1;
        [view.layer addSublayer:layer];
        
        startPoint = endPoint;
    }
    
}

#pragma mark -- 将等级转为对应的数字
- (NSMutableArray *)getLevelToNumber:(NSArray *)array
{
    NSMutableArray *dataArray = [[NSMutableArray alloc]init];
    int score = 0;
    for (NSString *level in array) {
        if ([level isEqualToString:@"S"]) {
            score = 120;
        }else if ([level isEqualToString:@"A"]){
            score = 100;
        }else if ([level isEqualToString:@"B"]){
            score = 80;
        }else if ([level isEqualToString:@"C"]){
            score = 60;
        }else if ([level isEqualToString:@"D"]){
            score = 40;
        }else if ([level isEqualToString:@"E"]){
            score = 20;
        }
        [dataArray addObject:@(score)];
    }
    return dataArray;
}

#pragma mark -- 绘制虚线
- (void)drawLine:(CGPoint)p andIndex:(int)index andView:(UIView *)view
{
    //绘制虚线，当奇数的时候
    CGPoint point = p;
    CAShapeLayer *layer = [CAShapeLayer layer];
    [layer setStrokeColor:[UIColor orangeColor].CGColor];
    
    [layer setLineWidth:1];
    // [shapeLayer setLineJoin:kCALineJoinRound];
    
    //设置虚线的线宽及间距
    [layer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:3], nil]];
    
    //创建虚线绘制路径
    CGMutablePathRef path = CGPathCreateMutable();
    
    //设置y轴方向的虚线
    CGPathMoveToPoint(path, NULL, point.x, point.y);
    float tan =  0.86;
    if (index == 1)
    {
        CGPathAddLineToPoint(path, NULL, point.x, point.y + 2*r);
    }
    else if (index == 3)
    {
        CGPathAddLineToPoint(path, NULL, point.x-2*r*tan, point.y - r);
    }
    else if (index == 5)
    {
        CGPathAddLineToPoint(path, NULL, point.x+2*r*tan, point.y - r);
    }
    
    //设置x轴方向的虚线
    //            CGPathMoveToPoint(path, NULL, point.x, point.y);
    //            CGPathAddLineToPoint(path, NULL, 1.3*Margin, point.y);
    
    //设置虚线绘制路径
    [layer setPath:path];
    CGPathRelease(path);
    [view.layer addSublayer:layer];
}
@end
